using System;
using System.IO;
using System.Text.Json;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace AuditionApp.Pages.Simulation
{
    public partial class SimulationPage : Page
    {
        private class TechniqueData
        {
            public string? Title { get; set; }
            public string[]? Content { get; set; }
        }

        public SimulationPage()
        {
            InitializeComponent();
        }

        private void QuestionTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (QuestionTypeComboBox.SelectedItem is not ComboBoxItem selectedItem)
            {
                TechniquePanel.Visibility = Visibility.Collapsed;
                return;
            }

            string selectedType = selectedItem.Content.ToString() ?? string.Empty;
            string jsonFile = "";

            switch (selectedType)
            {
                case "综合分析":
                    jsonFile = "ComprehensiveAnalysis.json";
                    break;
                case "人际关系应急应变":
                    jsonFile = "InterpersonalEmergency.json";
                    break;
                case "组织管理":
                    jsonFile = "Organizationment.json";
                    break;
            }

            if (!string.IsNullOrEmpty(jsonFile))
            {
                LoadTechniqueFromJson(jsonFile);
                TechniquePanel.Visibility = Visibility.Visible;
            }
        }

        private void LoadTechniqueFromJson(string jsonFile)
        {
            try
            {
                // 获取项目根目录
                string projectRoot = AppDomain.CurrentDomain.BaseDirectory;
                // 向上查找直到找到项目根目录
                while (!Directory.Exists(Path.Combine(projectRoot, "Pages")))
                {
                    var parent = Directory.GetParent(projectRoot);
                    if (parent == null)
                    {
                        throw new DirectoryNotFoundException("无法找到项目根目录");
                    }
                    projectRoot = parent.FullName;
                }

                string jsonPath = Path.Combine(projectRoot, "Pages", "Simulation", jsonFile);
                if (!File.Exists(jsonPath))
                {
                    throw new FileNotFoundException($"找不到文件：{jsonPath}");
                }

                string jsonContent = File.ReadAllText(jsonPath);
                var technique = JsonSerializer.Deserialize<TechniqueData>(jsonContent);
                if (technique?.Content == null)
                {
                    throw new InvalidDataException("JSON 数据格式不正确");
                }

                TechniqueContent.Inlines.Clear();
                foreach (var line in technique.Content)
                {
                    // 使用 ** 来标记需要加粗的文本
                    var parts = line.Split(new[] { "**" }, StringSplitOptions.None);
                    for (int i = 0; i < parts.Length; i++)
                    {
                        if (i % 2 == 0)
                        {
                            // 普通文本
                            TechniqueContent.Inlines.Add(new Run(parts[i]));
                        }
                        else
                        {
                            // 加粗文本
                            TechniqueContent.Inlines.Add(new Run(parts[i]) { FontWeight = FontWeights.Bold });
                        }
                    }
                    TechniqueContent.Inlines.Add(new LineBreak());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载解题技巧失败：{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
} 